//
//  CLSplashView.swift
//  CLEasyBBQ
//
//  Created by simon zhao on 2017/4/30.
//  Copyright © 2017年 simon zhao. All rights reserved.
//

import UIKit

protocol btnClickDelegate {
    
    func pushNewMainVc(sender:UIButton!)
}

class CLSplashView: UIScrollView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    fileprivate let numOfPages = 3
    var enterBtn : UIButton!
    var enterDelegate : btnClickDelegate!
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.isPagingEnabled = true
        self.showsHorizontalScrollIndicator = false
        self.scrollsToTop = false
        self.bounces = false
        self.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages),height:frame.size.height)
        for index in 0..<numOfPages {
            
            let CLImageView = UIImageView(image:UIImage.init(named: "GuideImage00\(index + 1)"))
            CLImageView.frame = CGRect(x:frame.size.width * CGFloat(index),y:0,width:frame.size.width,height:frame.size.height)
            self.addSubview(CLImageView)
            
            if index == 2 {
                
                CLImageView.isUserInteractionEnabled = true
                enterBtn = UIButton()
                enterBtn.layer.cornerRadius = 5
                enterBtn.layer.borderColor = UIColor.white.cgColor
                enterBtn.layer.borderWidth = 2
                enterBtn.setTitle("立即体验", for: .normal)
                enterBtn.addTarget(self, action: #selector(intoMainVc), for: .touchUpInside)
                CLImageView.addSubview(enterBtn)
                
                enterBtn.snp.makeConstraints { (make) in
        
                    make.centerX.equalTo(CLImageView)
                    make.top.equalTo(CLImageView).offset(self.frame.size.height * 0.84)
                    make.width.equalTo(screenWidth * 0.25)
                    make.height.equalTo(screenWidth * 0.1)
                }
                
            }
        }
        
        
    }
    
    @objc private func intoMainVc(sender : UIButton) {
        
        if enterDelegate != nil {
            
            enterDelegate.pushNewMainVc(sender:sender)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    
//        enterBtn.snp.makeConstraints { (make) in
//            
//            make.centerX.equalTo(self)
//            make.top.equalTo(self).offset(self.frame.size.height * 0.85)
//            make.width.equalTo(screenWidth * 0.25)
//            make.height.equalTo(screenWidth * 0.1)
//        }
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
